﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class MouseMoveNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            MouseMoveNParam? param = item.ControlParam as MouseMoveNParam;
            string posx = param.PosX;
            string posy = param.PosY;
            this.GetStringVar(ref posx, globleData);
            this.GetStringVar(ref posy, globleData);
            int x = int.Parse(posx);
            int y = int.Parse(posy);
            moveWithTail(x, y);
            return currentIndex;
        }
		
		//有轨迹移动鼠标
        public void moveWithTail(int x,int y) {
            Point current = Cursor.Position;
            int cx =current.X;
            int cy =current.Y;
            //步长
            double setpLX =(x - cx) / 500D;
            double setpLY =(y - cy) / 500D;
            double totalX = 0;
            double totalY = 0;
            for(int i = 1; i <= 500; i++){
                totalX += setpLX;
                totalY += setpLY;
                if(Math.Abs(totalX) >= 1) {
                    cx += (int)totalX;
                    totalX -= (int)totalX;
                }
                if(Math.Abs(totalY) >= 1) { 
                    cy += (int)totalY;
                    totalY -= (int)totalY;
                }
                User32Utils.SetCursorPos(cx, cy);
                Thread.Sleep(1);
            }
            //最后直接移动到目标位置
            User32Utils.SetCursorPos(x, y);
        }
    }
}
